﻿using Adp;
using Microsoft.AspNetCore.Mvc;
using Yqd.Models;
using System.Linq;
using Yqd.Controllers.Filter;
using Adp.Common;
using System;
using Yqd.Core;
using System.Collections.Generic;

namespace Yqd.Controllers
{
    [Area("Yqd")]
    public class WxSpecialTaskController : Controller
    {
        YqdContext ctx;

        public WxSpecialTaskController(YqdContext ctx)
        {
            this.ctx = ctx;
        }

        [User]
        public ActionResult GetList()
        {
            var key = Form.GetString("key");
            var page = Form.GetInt("page", 1);
            var pagesize = Form.GetInt("pagesize", 0);
            var r = ctx.Yqd_SpecialTask.AsQueryable();
            if (key.Trim() != "")
            {
                r = r.Where(it => it.Name.ToLower().Trim().Contains(key.ToLower().Trim()));
            }
            return this.ToJson(new
            {
                success = true,
                totalcount=r.Count(),
                data = r.OrderBy(it=>it.Tactic).Skip((page - 1) * pagesize).Take(pagesize).ToList().Select(it => new
                {
                    it.TaskID,
                    it.Name,
                    it.Remark,
                    Instime = it.Instime.ToString("yyyy-MM-dd")
                })
            });
        }

        [User]
        public ActionResult GetTaskList()
        {
            return this.ToJson(new
            {
                success = true,
                data = ctx.Yqd_SpecialTask.OrderBy(it => it.Tactic).ToList().Select(it => new
                {
                    text = it.Name,
                    value = it.TaskID
                })
            });
        }

        /// <summary>
        /// 添加专项任务
        /// </summary>
        /// <returns></returns>
        [User]
        public IActionResult Add(Yqd_SpecialTask model)
        {
            model.Tactic = ctx.Database.MaxID("Yqd_SpecialTask", "Tactic");
            model.Instime = DateTime.Now;
            model.EmployeeCode = WxUser.LogonInfo.EmployeeCode;
            model.UnitCode = WxUser.LogonInfo.UnitCode;
            ctx.Yqd_SpecialTask.Add(model);
            var ret = ctx.SaveChanges();
            return this.ToJson(new
            {
                success = true,
                ret
            });
        }

        /// <summary>
        /// 修改专项任务
        /// </summary>
        /// <returns></returns>
        [User]
        public IActionResult Edit(Yqd_SpecialTask model)
        {
            var task = ctx.Yqd_SpecialTask.FirstOrDefault(it => it.TaskID == model.TaskID);
            if (task != null)
            {
                task.Name = model.Name;
                task.Remark = model.Remark;
            }
            var ret = ctx.SaveChanges();
            return this.ToJson(new
            {
                success = true,
                ret
            });
        }

        [User]
        public ActionResult Delete()
        {
            var TaskID = Form.GetInt("TaskID");
            var ret = ctx.Database.ExecuteSql("delete from Yqd_SpecialTask where TaskID={0};", TaskID);
            return this.ToJson(new
            {
                success = true,
                ret
            });
        }
    }
}
